package mop.bootstrap;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import mop.gui.GUI;
import mop.model.World;
import mop.model.user.Administrator;
import mop.security.acl.Acl;
import mop.xml.TaskplannerXMLReader;
import mop.xml.ThemeXMLReader;

/**
 * Starts our task manager
 *
 * @author  MOP Team 7
 */
public class Main
{
   /**
    * Simple bootstrap procedure.
    *
    * @param    args
    *           the command line arguments
    */
    public static void main(String args[])
    {
        java.awt.EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    // 1. creates world
                    World world = new World();

                    // 2. reads theme xml
                    ThemeXMLReader themeReader = new ThemeXMLReader(world);
                    String wd = System.getProperty("user.dir");
                    JFileChooser fc = new JFileChooser(wd);
                    int rc = fc.showDialog(null, "Select THEME File");
                    if (rc == JFileChooser.APPROVE_OPTION)
                        themeReader.process(fc.getSelectedFile());
                    else
                        System.exit(0);

                    // 3. reads taskplanner xml
                    TaskplannerXMLReader reader = new TaskplannerXMLReader(world);
                    wd = System.getProperty("user.dir");
                    fc = new JFileChooser(wd);
                    rc = fc.showDialog(null, "Select Data File");
                    if (rc == JFileChooser.APPROVE_OPTION)
                        reader.process(fc.getSelectedFile());
                    else
                        System.exit(0);
                    
                    // 4. Create administrator
                    try {
                    	new Administrator("Administrator", world.getUserDataBase());
                    } catch (Throwable t) {
                        JOptionPane.showMessageDialog(null, "An error occured!\n\n" + t.toString(), "Error", JOptionPane.ERROR_MESSAGE);
                    }

                    // 5. Create controllers and ACL
                    Acl acl = new Acl();
                    world.setAcl(acl);
                    ControllerACL ca = new ControllerACL();
                    ca.buildControllers(world, acl);
               
       
                    // 6. Create GUI
                    new GUI(world.getAvailableControllers());
                }
            });
    }
}